<?php

namespace app\api\controller;

use think\Db;
use WebGeeker\Validation\Validation;

//导航相关
class Navigation
{
    public $mongo_obj;

    public function __construct()
    {
        try {
            $this->mongo_obj = Db::connect("db_mongo");
        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }


    //获取首页和导航
    public function getNewsNavigation()
    {
        try {


            // 验证（如果验证不通过，会抛出异常）
            Validation::validate($_GET, [
                "navigation_type" => "Required|IntIn:1,2"
            ]);

            $mongo_obj = $this->mongo_obj->name('item_navigation');

            $navigation_type = (int)$_GET['navigation_type'];

            $result = $mongo_obj->where(['navigation_status' => 1, 'navigation_type' => $navigation_type])->field('navigation_name,scene_id')->select();

            successBack(['list' => $result]);

        } catch (\Exception $exception) {
            errorBack($exception);
        }
    }

    public function test()
    {

        $obj = $this->mongo_obj->name('item');

        $list = $obj->select();

        foreach ($list as $value) {
            $obj->where(['id' => $value['id']])->update(['video_skip_type'=>3]);
        }

    }

    //添加首页和导航
    private function addNavigation(){
        try {
            $item_navigation_obj = $this->mongo_obj->name('item_navigation');

            $addData = [];

            $addData['navigation_name'] = '推荐';

            $addData['navigation_status'] = 1;

            $addData['navigation_weight'] = 99;

            $addData['scene_id'] = 'b99';

            $addData['navigation_type'] = 2;

            $result = $item_navigation_obj->insertGetId($addData);

            if($result){
                echo '插入成功';
            }else{
                echo '插入失败';
            }
        }catch (\Exception $exception){
            errorBack($exception);
        }
    }

}
